// Copyright 2008 David Mitchell
// Released under the MIT License

using System;
using System.Collections.Generic;

namespace NSDuctTape
{
	/// <summary>
	/// I haven't played around with the most recent Mono bits enough to know how well it supports System.Core and Linq.
	/// Until I've done this, these methods will work well enough.
	/// </summary>
	internal static class Enumerable
	{
		public static IEnumerable<TOutput> Select<TInput, TOutput>(IEnumerable<TInput> list, Converter<TInput, TOutput> convert)
		{
			foreach (TInput value in list)
				yield return convert(value);
		}

		public static IEnumerable<TOutput> SelectMany<TInput, TOutput>(IEnumerable<TInput> list, Converter<TInput, IEnumerable<TOutput>> convert)
		{
			foreach (TInput value in list)
				foreach (TOutput converted in convert(value))
					yield return converted;
		}

		public static IEnumerable<T> Where<T>(IEnumerable<T> list, Predicate<T> predicate)
		{
			foreach (T value in list)
				if (predicate(value))
					yield return value;
		}

		public static List<T> ToList<T>(IEnumerable<T> list)
		{
			List<T> result = list as List<T>;
			return result ?? new List<T>(list);
		}

		public static T[] ToArray<T>(IEnumerable<T> list)
		{
			return ToList(list).ToArray();
		}
	}
}
